package com.naiterui.ehp.bp.utils.biz;

import java.util.HashMap;
import java.util.Map;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;

/**
 * 调度任务Utils
 *
 * @author Gaoll
 * @since HZD-1.0.0
 */
public class ScheduleUtils {

    /**
     * B2C-订单自动发货
     */
    public static final int B2C_ORDER_AUTO_PROCESS = 14;
    /**
     * 患者accessToken刷新任务
     */
    public static final int TASK_TYPE_ACCESS_TOKEN = 15;
    /**
     * 代表公众号accessToken刷新任务
     */
    public static final int TASK_TYPE_DY_ACCESS_TOKEN = 17;

    public static final int TASK_TYPE_ESIGN_ACCESS_TOKEN = 18;
    /**
     * 患者用药提醒任务
     */
    public static final int TASK_TYPE_DRUG_REMIND = 16;

    /**
     * 视频咨询任务
     */
    public static final int TASH_TYPE_VIDEO_CONSULT = 19;

    /**
     * 处方自动审核
     */
    public static final int TASH_TYPE_AUTO_AUDIT = 20;

    /**
     * 处方调配药师审核
     */
    public static final int TASH_TYPE_ALLOCATE_AUDIT = 21;

    /**
     * 随访任务开始
     */
    public static final int TASH_TYPE_FOLLOW_UP = 22;

    private static final String COMMON_TASK_ADD = "/commonTask/add";
    private static final String COMMON_TASK_CANCEL = "/commonTask/cancel";
    private static final String CUSTOMER_TASK_ADD = "/customerTask/add";
    private static final String CUSTOMER_TASK_CANCEL = "/customerTask/cancel";


    /**
     * 添加通用任务
     *
     * @param hosts      task服务host
     * @param invokeTime 触发时间
     * @param timeStamp  通用任务时间戳
     * @param callBack   业务回调URL
     * @param paramMap   业务回调参数
     *
     * @since HZD-1.0.0
     */
    public static void addCommonTask(String hosts, String invokeTime, Long timeStamp, String callBack,
                                     Map<String, String> paramMap) throws BusinessException {

        // String invokeTime = DateUtil.formatDate(invokeDate, DateUtil.CRON_TIME_FORMAT);
        Map<String, String> params = new HashMap<>();
        params.put("timeStamp", timeStamp + "");
        params.put("invokeTime", invokeTime);
        params.put("callBack", callBack);
        if (null != paramMap && !paramMap.isEmpty()) {
            params.put("paramJson", JsonMapper.toJson(paramMap));
        }
        String postUrl = hosts.concat(COMMON_TASK_ADD);

        RestHttpUtil.post().url(postUrl).paramMap(params).executeData();
    }

    /**
     * 移除通用任务
     * ScheduleUtils.reomveCommonTask()
     *
     * @param hosts     task服务host
     * @param timeStamp 通用任务时间戳
     */
    public static void removeCommonTask(String hosts, Long timeStamp) throws BusinessException {

        Map<String, String> params = new HashMap<>();
        params.put("timeStamp", timeStamp + "");

        String postUrl = hosts.concat(COMMON_TASK_CANCEL);
        RestHttpUtil.post().url(postUrl).paramMap(params).executeData();
    }

    /**
     * 添加自定义任务
     *
     * @param hosts      task服务host
     * @param taskType   自定义任务类型(TASK_TYPE_*)
     * @param invokeTime 触发时间
     * @param unionId    自定义任务业务id
     * @param callBack   业务回调URL
     * @param paramMap   业务回调参数
     */
    public static void addCustomerTask(String hosts, String invokeTime, Integer taskType,
                                       String unionId, String callBack, Map<String, String> paramMap) throws BusinessException {

        // String invokeTime = DateUtil.formatDate(invokeDate, DateUtil.CRON_TIME_FORMAT);
        Map<String, String> params = new HashMap<>();
        params.put("invokeTime", invokeTime);
        params.put("taskType", taskType.toString());
        params.put("unionId", unionId);
        params.put("callBack", callBack);
        if (null != paramMap && !paramMap.isEmpty()) {
            params.put("paramJson", JsonMapper.toJson(paramMap));
        }
        String postUrl = hosts.concat(CUSTOMER_TASK_ADD);
        RestHttpUtil.post().url(postUrl).paramMap(params).executeData();
    }

    /**
     * 移除自定义任务
     * ScheduleUtils.removeCustomerTask()
     *
     * @param hosts    task服务host
     * @param taskType 自定义任务类型(TASK_TYPE_*)
     * @param unionId  自定义任务业务id
     *
     * @return
     *
     * @Author Gaoll
     * @Date 2016年7月28日
     * @since HZD-1.0.0
     */
    public static void removeCustomerTask(String hosts, Integer taskType, String unionId) throws BusinessException {

        // String invokeTime = DateUtil.formatDate(invokeDate, DateUtil.CRON_TIME_FORMAT);
        Map<String, String> params = new HashMap<>();
        params.put("taskType", taskType.toString());
        params.put("unionId", unionId);
        String postUrl = hosts.concat(CUSTOMER_TASK_CANCEL);

        RestHttpUtil.post().url(postUrl).paramMap(params).executeData();
    }

}
